package xrz.xuanbao.modular.mine.myshoucang;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import xrz.cuanbao.R;
import xrz.cuanbao.activity.Act_RenRenCuanItem_new;
import xrz.cuanbao.https.Http;
import xrz.cuanbao.https.HttpConst;
import xrz.cuanbao.https.HttpListener;
import xrz.cuanbao.https.HttpUtil;
import xrz.cuanbao.modular.reg_login.User;
import xrz.cuanbao.utils.UserUtils;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xrz.sxm.base.activity.BaseAct;

public class Act_RenQiShouCang extends BaseAct implements OnItemLongClickListener, OnItemClickListener {

	private Adapter_RenQi adapter;
	@ViewInject(R.id.renqishoucang_gridview)
	private GridView mGridView;

	private String type;

	@Override
	protected int setContentView() {
		return R.layout.layout_renqishoucang;
	}

	@Override
	protected void initData() {
		type = getIntent().getStringExtra("type");
		adapter = new Adapter_RenQi(this);
		mGridView.setAdapter(adapter);
		mGridView.setOnItemLongClickListener(this);
		mGridView.setOnItemClickListener(this);
		getRenqi();
	}

	@Override
	protected String setTitle() {
		return null;
	}

	@Override
	protected boolean topViewHide() {
		return true;
	}

	@Override
	protected boolean isBlueTitle() {
		return false;
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		adapter.getList().get(arg2).flag = true;
		adapter.notifyDataSetChanged();
		return false;
	}

	private void getRenqi() {
		Http http = new Http(m_ctx);
		User user = UserUtils.getUser(m_ctx);
		Map<String, String> map = HttpUtil.getParams("username", user.username, "type", type);
		http.sendRequest(HttpConst.my_collect, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				List<RenqiShoucang> list = JSON.parseArray(jsonObject.getString("data"), RenqiShoucang.class);
				adapter.appendToList(list);
			}

			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Bundle bundle = new Bundle();
		bundle.putString("id", adapter.getList().get(position).itemid);
		skip(Act_RenRenCuanItem_new.class, bundle);
	}
}
